package edu.xhu.common.threadpool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

//注:不同功能使用不同的线程池
@Configuration
public class ThreadPoolConfig {

    /**
     * 评论线程池
     */
    @Bean(value = "commentExecutor")
    public Executor commentExecutor() {
        ThreadPoolTaskExecutor commentExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数信息
        commentExecutor.setCorePoolSize(10);   //核心线程数
        commentExecutor.setMaxPoolSize(50);    //最大线程数
        commentExecutor.setQueueCapacity(200); //阻塞队列容量
        commentExecutor.setKeepAliveSeconds(60);//心跳60s
        commentExecutor.setThreadNamePrefix("commentExecutor--");
        commentExecutor.setWaitForTasksToCompleteOnShutdown(true);
        commentExecutor.setAwaitTerminationSeconds(60);
        //修改拒绝策略为使用当前线程执行
        commentExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化线程池
        commentExecutor.initialize();
        return commentExecutor;
    }

    @Bean(value = "checkCommitExecutor")
    public Executor checkCommitExecutor() {
        ThreadPoolTaskExecutor checkCommitExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数信息
        checkCommitExecutor.setCorePoolSize(20);   //核心线程数
        checkCommitExecutor.setMaxPoolSize(100);    //最大线程数
        checkCommitExecutor.setQueueCapacity(500); //阻塞队列容量
        checkCommitExecutor.setKeepAliveSeconds(60);//心跳60s
        checkCommitExecutor.setThreadNamePrefix("checkExecutor--");
        checkCommitExecutor.setWaitForTasksToCompleteOnShutdown(true);
        checkCommitExecutor.setAwaitTerminationSeconds(60);
        //修改拒绝策略为使用当前线程执行
        checkCommitExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化线程池
        checkCommitExecutor.initialize();
        return checkCommitExecutor;
    }

    @Bean(value = "checkExecutor")
    public Executor checkExecutor() {
        ThreadPoolTaskExecutor checkExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数信息
        checkExecutor.setCorePoolSize(20);   //核心线程数
        checkExecutor.setMaxPoolSize(100);    //最大线程数
        checkExecutor.setQueueCapacity(500); //阻塞队列容量
        checkExecutor.setKeepAliveSeconds(60);//心跳60s
        checkExecutor.setThreadNamePrefix("checkExecutor--");
        checkExecutor.setWaitForTasksToCompleteOnShutdown(true);
        checkExecutor.setAwaitTerminationSeconds(60);
        //修改拒绝策略为使用当前线程执行
        checkExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化线程池
        checkExecutor.initialize();
        return checkExecutor;
    }

    @Bean(value = "chatExecutor")
    public Executor chatExecutor() {
        ThreadPoolTaskExecutor chatExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数信息
        chatExecutor.setCorePoolSize(20);   //核心线程数
        chatExecutor.setMaxPoolSize(100);    //最大线程数
        chatExecutor.setQueueCapacity(500); //阻塞队列容量
        chatExecutor.setKeepAliveSeconds(60);//心跳60s
        chatExecutor.setThreadNamePrefix("checkExecutor--");
        chatExecutor.setWaitForTasksToCompleteOnShutdown(true);
        chatExecutor.setAwaitTerminationSeconds(60);
        //修改拒绝策略为使用当前线程执行
        chatExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化线程池
        chatExecutor.initialize();
        return chatExecutor;
    }

}
